package com.doraro.platform.config.shiro;

import com.doraro.platform.common.utils.JsonUtil;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.crazycake.shiro.RedisCacheManager;
import org.crazycake.shiro.RedisManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    @Autowired
    private JsonUtil mapper;
    @Value("${spring.redis.host}")
    private String redisHost;
    @Value("${spring.redis.port}")
    private String redisPort;

    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition() {
        DefaultShiroFilterChainDefinition filterMap = new DefaultShiroFilterChainDefinition();
        final LinkedHashMap<String, String> map = new LinkedHashMap<>(16);
        map.put("/webjars/**", "anon");
        map.put("/druid/**", "anon");
        map.put("/sys/login", "anon");
        map.put("/swagger/**", "anon");
        map.put("/v2/api-docs", "anon");
        map.put("/swagger-ui.html", "anon");
        map.put("/swagger-resources/**", "anon");
        map.put("/captcha.jpg", "anon");
        map.put("/api/user/logout", "oauth2");
        //api user用户注册登录，不需要权限
        map.put("/api/user/**", "anon");
        map.put("/**", "oauth2");
        filterMap.addPathDefinitions(map);
        return filterMap;
    }

    /**
     * @param sessionManager
     * @param definition
     * @return
     */
    @Bean
    public ShiroFilterFactoryBean shirFilter(SecurityManager sessionManager, ShiroFilterChainDefinition definition) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(sessionManager);
        //oauth过滤
        Map<String, Filter> filters = new HashMap<>(1);
        final TokenFilter filter = new TokenFilter();
        final RestPermFilter permFilter = new RestPermFilter();
        filter.setUtil(mapper);
        permFilter.setUtil(mapper);
        filters.put("oauth2", filter);
        filters.put("rperm", permFilter);
        shiroFilter.setFilters(filters);
        shiroFilter.setFilterChainDefinitionMap(definition.getFilterChainMap());
        return shiroFilter;
    }

    @Bean
    public RedisCacheManager redisCacheManager() {
        RedisCacheManager redisCacheManager = new RedisCacheManager();
        redisCacheManager.setRedisManager(redisManager());
        redisCacheManager.setExpire(600);
        redisCacheManager.setPrincipalIdFieldName("id");
        return redisCacheManager;
    }

    @Bean
    public RedisManager redisManager() {
        RedisManager redisManager = new RedisManager();
        redisManager.setHost(redisHost + ":" + redisPort);
        return redisManager;
    }


}
